package persistence;

// default package
// Generated Oct 13, 2014 5:58:06 PM by Hibernate Tools 4.0.0

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import entity.Profile;

/**
 * Home object for domain model class Profile.
 * 
 * @see .Profile
 * @author Hibernate Tools
 */
@Stateless
public class ProfileDAO {

	private static final Log log = LogFactory.getLog(ProfileDAO.class);

	Session session;
	Transaction tx;

	public static void main(String[] args) {
		ProfileDAO profile = new ProfileDAO();
//		profile.addProfile("Mahesh", "Bonagiri");
		try {
			profile.getCounts("8P2");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/* Method to CREATE an employee in the database */
	public Integer addProfile(String fname, String lname) {
		session = SessionFactoryUtil.getSessionFactory().openSession();
		Transaction tx = null;
		Integer profileID = null;
		try {
			tx = session.beginTransaction();
			Profile profile = new Profile();
			profile.setFirstname(fname);
			profile.setLastname(lname);
			profile.setTenantid("8P2");
			profileID = (Integer) session.save(profile);
			tx.commit();
		} catch (HibernateException e) {
			if (tx != null)
				tx.rollback();
			e.printStackTrace();
		} finally {
			session.close();
		}
		return profileID;
	}

	public Integer persist(Profile transientInstance) throws Exception {
		log.debug("persisting Profile instance");
		Integer profileID = null;
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			profileID = (Integer)session.save(transientInstance);
			tx.commit();
			log.debug("persist successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
		return profileID;
	}

	public void remove(Profile persistentInstance) throws Exception {
		log.debug("removing Profile instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			session.delete(persistentInstance);
			tx.commit();
			log.debug("remove successful");
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("remove failed", ex);
			throw ex;
		}
	}

	public Profile merge(Profile detachedInstance) throws Exception {
		log.debug("merging Profile instance");
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Profile result = (Profile) session.merge(detachedInstance);
			tx.commit();
			log.debug("merge successful");
			return result;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("merge failed", ex);
			throw ex;
		}
	}

	public Profile findById(int id, String tenantId) throws Exception {
		log.debug("getting Profile instance with id: " + id);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery("from Profile where idProfile ="
					+ id + " and tenantid='" + tenantId + "'");
			List<Profile> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list.get(0);
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}
	public List<Long> getCounts(String tenantId) throws Exception {
		log.debug("getting Profile counts : " );
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			Calendar lstWkCalendar=Calendar.getInstance();
			lstWkCalendar.add(Calendar.DATE, -7);
			Date lstWkDate=lstWkCalendar.getTime();
			Calendar lstMthCalendar=Calendar.getInstance();
			lstMthCalendar.add(Calendar.MONTH, -1);
			Date lstMthDate=lstMthCalendar.getTime();
			Calendar lstYrCalendar=Calendar.getInstance();
			lstYrCalendar.add(Calendar.YEAR, -1);
			Date lstYrDate=lstYrCalendar.getTime();
			Long lstWkQuery = ((Long)session.createQuery("select count(*) from Profile where reccrtts > "+sdf.format(lstWkDate)+" and tenantid='" + tenantId + "'").iterate().next()).longValue();
			Long lstMthQuery = ((Long)session.createQuery("select count(*) from Profile where reccrtts > "+sdf.format(lstMthDate)+" and tenantid='" + tenantId + "'").iterate().next()).longValue();
			Long lstYrQuery = ((Long)session.createQuery("select count(*) from Profile where reccrtts > "+sdf.format(lstYrDate)+" and tenantid='" + tenantId + "'").iterate().next()).longValue();
			
			Long lstWkUpdtQuery = ((Long)session.createQuery("select count(*) from Profile where reclstupdatets > "+sdf.format(lstWkDate)+"and tenantid='" + tenantId + "'").iterate().next()).longValue();
			Long lstMthUpdtQuery = ((Long)session.createQuery("select count(*) from Profile where reclstupdatets > "+sdf.format(lstMthDate)+"and tenantid='" + tenantId + "'").iterate().next()).longValue();
			Long lstYrUpdtQuery = ((Long)session.createQuery("select count(*) from Profile where reclstupdatets > "+sdf.format(lstYrDate)+"and tenantid='" + tenantId + "'").iterate().next()).longValue();
			System.out.println(lstWkQuery);
			System.out.println(lstMthQuery);
			System.out.println(lstYrQuery);
			System.out.println(lstWkUpdtQuery);
			System.out.println(lstMthUpdtQuery);
			System.out.println(lstYrUpdtQuery);
			List<Long> list=new ArrayList<Long>();
			list.add(lstWkQuery);
			list.add(lstMthQuery);
			list.add(lstYrQuery);
			list.add(lstWkUpdtQuery);
			list.add(lstMthUpdtQuery);
			list.add(lstYrUpdtQuery);
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("get failed", ex);
			throw ex;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<Profile> search(String queryStr) throws Exception {
		log.debug("getting Profile instance with search: " + queryStr);
		try {
			session = SessionFactoryUtil.getSessionFactory()
					.getCurrentSession();
			tx = session.beginTransaction();
			Query query = session.createQuery(queryStr);
			List<Profile> list = query.list();
			tx.commit();
			log.debug("get successful");
			return list;
		} catch (Exception ex) {
			session.close();
			if (tx != null) {
				tx.rollback();
			}
			log.error("persist failed", ex);
			throw ex;
		}
	}
	
	
}
